package com.br.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import com.br.controller.UserControl;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login extends JDialog {

	public String user;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtUser;
	private JPasswordField txtSenha;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Login dialog = new Login();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Login() {
		final Splash splash = new Splash();
		
		setBounds(100, 100, 512, 338);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel label = new JLabel("Informe o Usu\u00E1rio e Senha");
			label.setFont(new Font("Verdana", Font.PLAIN, 25));
			label.setBounds(10, 11, 347, 58);
			contentPanel.add(label);
		}
		{
			JLabel label = new JLabel("Usu\u00E1rio:");
			label.setFont(new Font("Verdana", Font.PLAIN, 15));
			label.setBounds(10, 128, 72, 14);
			contentPanel.add(label);
		}
		{
			txtUser = new JTextField();
			txtUser.setColumns(10);
			txtUser.setBounds(92, 127, 209, 20);
			contentPanel.add(txtUser);
		}
		{
			JLabel label = new JLabel("Senha:");
			label.setFont(new Font("Verdana", Font.PLAIN, 15));
			label.setBounds(10, 182, 72, 14);
			contentPanel.add(label);
		}
		{
			txtSenha = new JPasswordField();
			txtSenha.setBounds(92, 181, 209, 20);
			contentPanel.add(txtSenha);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						UserControl userControl = splash.userControl;
						
						user = txtUser.getText();
						String password = String.valueOf(txtSenha.getPassword());
						
						String log = userControl.logar(user, password);
						
						if(log == "sucesso"){
							TelaInicial inicial = new TelaInicial();
							inicial.setTitle(inicial.getTitle()+"  -  "+txtUser.getText().toUpperCase()+'.');
							inicial.setExtendedState(JFrame.MAXIMIZED_BOTH);
							inicial.setCorrentUser(txtUser.getText());
							inicial.verificaAdmin(txtUser.getText());
							inicial.setVisible(true);
							dispose();
						}
						
						if(log == "erroUser"){
							System.out.println("Erro!");
						}
					}
				});
				okButton.setFont(new Font("Tahoma", Font.PLAIN, 10));
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						System.exit(0);
					}
				});
				cancelButton.setFont(new Font("Tahoma", Font.PLAIN, 10));
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
